#ifndef _FSIP3_H_
#define _FSIP3_H_

#include "Equations/TwoPhaseEquation.hpp"

namespace Tuna {

  template <typename T, int Dim> class FSIP3;

  template <typename T>
  struct Typeinfo <FSIP3<T, 1> > {
    typedef T prec_t;
    enum { Dim = 1 };
  };

  template <typename T>
  struct Typeinfo <FSIP3<T, 2> > {
    typedef T prec_t;
    enum { Dim = 2 };
  };

  template <typename T>
  struct Typeinfo <FSIP3<T, 3> > {
    typedef T prec_t;
    enum { Dim = 3 };
  };

  
  template<typename Tprec, int Dim>
  class FSIP3 : public TwoPhaseEquation<FSIP3<Tprec, Dim> >
  {

    typedef TwoPhaseEquation<FSIP3<Tprec, Dim> > TP_FSIP3;
    
    using GeneralEquation< TP_FSIP3 >::aE;
    using GeneralEquation< TP_FSIP3 >::aW;
    using GeneralEquation< TP_FSIP3 >::aN;
    using GeneralEquation< TP_FSIP3 >::aS;
    using GeneralEquation< TP_FSIP3 >::aF;
    using GeneralEquation< TP_FSIP3 >::aB;
    using GeneralEquation< TP_FSIP3 >::aP;
    using GeneralEquation< TP_FSIP3 >::sp;
    using GeneralEquation< TP_FSIP3 >::dx;
    using GeneralEquation< TP_FSIP3 >::dy;
    using GeneralEquation< TP_FSIP3 >::dz;
    using GeneralEquation< TP_FSIP3 >::bi;
    using GeneralEquation< TP_FSIP3 >::ei;
    using GeneralEquation< TP_FSIP3 >::bj;
    using GeneralEquation< TP_FSIP3 >::ej;
    using GeneralEquation< TP_FSIP3 >::bk;
    using GeneralEquation< TP_FSIP3 >::ek;
    using GeneralEquation< TP_FSIP3 >::applyBoundaryConditions1D;
    using GeneralEquation< TP_FSIP3 >::applyBoundaryConditions2D;
    using GeneralEquation< TP_FSIP3 >::applyBoundaryConditions3D;

    using TP_FSIP3::S;
    using TP_FSIP3::phi_0;
    using TP_FSIP3::Srw;
    using TP_FSIP3::Sro;
    using TP_FSIP3::mu_w;
    using TP_FSIP3::mu_o;
    using TP_FSIP3::k;
    using TP_FSIP3::injection;

  public:
    typedef Tprec prec_t;
    typedef typename TunaArray<prec_t, Dim >::huge ScalarField;
    
    FSIP3() : TwoPhaseEquation<FSIP3<prec_t, Dim > >() { }    
    ~FSIP3() { };
    
    inline bool calcCoefficients1D(); 
    inline bool calcCoefficients2D() { };
    inline bool calcCoefficients3D() ;
    inline void printInfo() { std::cout << " FSIP3 "; }
  };

/*
 *  Lineal for realtive permeability , Average for Sw
 */
template<typename Tprec, int Dim>
inline bool FSIP3<Tprec, Dim>::calcCoefficients1D () 
{
    static prec_t Sw_e, Sw_w;

    // Lineal 
    static prec_t mult_o = k / (1 - Srw - Sro) * mu_o * dx ;
    static prec_t mult_w = k / (1 - Srw - Sro) * mu_w * dx ;
    
    aE = 0.0; aW = 0.0; aP = 0.0; sp = 0.0;

    for (int i =  bi; i <= ei; ++i) {      

      // Average
      Sw_e = ( S(i+1) + S(i) ) * 0.5;
      Sw_w = ( S(i-1) + S(i) ) * 0.5;      

      // Lineal
      aE (i) = (1 - Sro - Sw_e) * mult_o + (Sw_e - Srw) * mult_w ;
      aW (i) = (1 - Sro - Sw_w) * mult_o + (Sw_w - Srw) * mult_w ;
      aP (i) = aE (i) + aW (i);      
    }
    applyBoundaryConditions1D();
    return 0;
}

/*
 *  Lineal for relative permeability , Upwind for Sw
 */
template<typename Tprec, int Dim>
inline bool FSIP3<Tprec, Dim>::calcCoefficients3D () 
{
    static prec_t Sw_e, Sw_w, Sw_n, Sw_s, Sw_f, Sw_b;

    // Lineal 
    static prec_t mult_o = k / ( (1 - Srw - Sro) * mu_o ) ;
    static prec_t mult_w = k / ( (1 - Srw - Sro) * mu_w ) ;
    static prec_t dxdz_dy = dx * dz / dy;
    static prec_t dydz_dx = dy * dz / dx;
    static prec_t dxdy_dz = dx * dy / dz;

    aE = 0.0; aW = 0.0; aN = 0.0; aS = 0.0; aF = 0.0; aB = 0.0;
    aP = 0.0; sp = 0.0;

    for (int ki = bk; ki <= ek; ++ki) 
      for (int i =  bi; i <= ei; ++i) 
	for (int j = bj; j <= ej; ++j) {
	  // Average
	  Sw_e = ( S(i+1,j,ki) + S(i,j,ki) ) * 0.5;
	  Sw_w = ( S(i-1,j,ki) + S(i,j,ki) ) * 0.5;   
	  Sw_n = ( S(i,j+1,ki) + S(i,j,ki) ) * 0.5;
	  Sw_s = ( S(i,j-1,ki) + S(i,j,ki) ) * 0.5; 
	  Sw_f = ( S(i,j,ki+1) + S(i,j,ki) ) * 0.5;
	  Sw_b = ( S(i,j,ki-1) + S(i,j,ki) ) * 0.5; 
	  
	  // Lineal
	  aE (i, j, ki) = ( (1 - Sro - Sw_e) * mult_o + 
			   (Sw_e - Srw) * mult_w ) * dydz_dx;
	  aW (i, j, ki) = ( (1 - Sro - Sw_w) * mult_o + 
			   (Sw_w - Srw) * mult_w ) * dydz_dx;
	  aN (i, j, ki) = ( (1 - Sro - Sw_n) * mult_o + 
			   (Sw_n - Srw) * mult_w ) * dxdz_dy;
	  aS (i, j, ki) = ( (1 - Sro - Sw_s) * mult_o + 
			   (Sw_s - Srw) * mult_w ) * dxdz_dy;
	  aF (i, j, ki) = ( (1 - Sro - Sw_f) * mult_o + 
			   (Sw_f - Srw) * mult_w ) * dxdy_dz;
	  aB (i, j, ki) = ( (1 - Sro - Sw_b) * mult_o + 
			   (Sw_b - Srw) * mult_w ) * dxdy_dz;	  
	  aP (i, j, ki) = aE (i, j, ki) + aW (i, j, ki) + 
	                  aN (i, j, ki) + aS (i, j, ki) + 
	                  aF (i, j, ki) + aB (i, j, ki);
	}        
    applyBoundaryConditions3D();


    // Source for injection : 
    // (\frac{\bar{q}_w}{\rho_w} + \frac{\bar{q}_w}{\rho_w}) * \Delta V
    sp (bi, bj, bk) +=  injection  ;

/*
    sp (ei, ej) +=  injection  ;
    sp (bi, ej) +=  injection  ;
    sp (ei, bj) +=  injection  ;
*/
    //Sink for extraction :
    sp (ei, ej, ek) -= injection  ;

//    sp (ei/2+1, ej/2+1) -= 4 * injection  ;

    return 0;
}



} // Tuna namespace


#endif //_FSIP3_H_

















